/**
 * 
 */
package com.android.hw.projectmenu;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import com.qualcomm.qcnvitems.QcNvItems;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.text.TextUtils;
import android.util.Log;

/**
 * @author gaoshengpan
 *
 */
public class LcdParamInfo extends PreferenceActivity {
	
	private static final String KEY_LCD_PARAM = "lcd_param";
	private static final String KEY_CAMERA_PARAM1 = "camera_param1";
	private static final String KEY_CAMERA_PARAM2 = "camera_param2";
	private static final String KEY_QCN_VERSION = "radio_frequency";
	private static String mUnknown = "null";
	private static String TAG = "LcdParamInfo";
    private QcNvItems mNv;
	
	private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.what) {

            case 2:
                try {
            		setSummaryText(KEY_QCN_VERSION, mNv.getQcnVersion());
        		} catch (Exception e) {
        			// TODO: handle exception
        			Log.d(TAG, "KEY_QCN_VERSION-----error");
        		}
            	break;
            default:
                break;
            }
        }
    };

	/**
	 * 
	 */
	public LcdParamInfo() {
		// TODO Auto-generated constructor stub
	}

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.lcd_param);
        mNv = new QcNvItems(this);
    }
	
	 /* (non-Javadoc)
		 * @see android.app.Activity#onResume()
		 */
		@Override
		protected void onResume() {
			// TODO Auto-generated method stub
			super.onResume();
			setLcdInfo();
		}
		
		 private void setLcdInfo() {
		       String lcd = "";
		       String camera1 = "";
		       String camera2 = "";
		       String[] items = new String[2];
		       items= readFile("/sys/class/graphics/fb0/panel_para_version").split("=");
		       lcd = items[1];
		       camera1 =readFile("/sys/class/camera/rear_camera/param") ;
		       camera2 =readFile("/sys/class/camera/front_camera/param") ;
			       setSummaryText(KEY_LCD_PARAM, lcd);
		    	   setSummaryText(KEY_CAMERA_PARAM1,  camera1);
		    	   setSummaryText(KEY_CAMERA_PARAM2,  camera2);
		    	   mHandler.sendEmptyMessage(2);
				
		    }
		
		private void setSummaryText(String preferenceKey, String value) {
	        Preference preference = findPreference(preferenceKey);
	        if (preference == null)
	            return;

	        if (TextUtils.isEmpty(value))
	            preference.setSummary(mUnknown);
	        else
	            preference.setSummary(value);
	    }
		
		public static String readFile(String filePath) {
			String res = "";
			try {
				BufferedReader br = new BufferedReader(new InputStreamReader(
						new FileInputStream(new File(filePath))));
				String str = null;
				while ((str = br.readLine()) != null) {
					res += str;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return res;
		}
		
		/*
	     * (non-Javadoc)
	     * 
	     * @see android.preference.PreferenceActivity#onDestroy()
	     */
	    @Override
	    protected void onDestroy() {
	        // TODO Auto-generated method stub
	        super.onDestroy();
	        if (mNv != null) {
	            mNv.dispose();
	        }
	    }
}
